package com.zhuangjie.coupon.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.zhuangjie.coupon.enums.CouponType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 *
 * </p>
 *
 * @author zhuangjie
 * @since 2024-03-17
 */
@Getter
@Setter
@Accessors(chain = true)
@TableName("coupon_batch")
@ApiModel(value = "CouponBatch对象", description = "")
public class CouponBatch implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("批次编号")
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    @ApiModelProperty("店铺id（仅店铺卷，平台卷null）")
    @TableField("shop_id")
    private Long shopId;

    @ApiModelProperty("优惠卷名称")
    @TableField("name")
    private String name;

    @ApiModelProperty("批次状态（0不可领取 1可领取）")
    @TableField("status")
    private Byte status;

    @ApiModelProperty("描述信息（如规则描述）")
    @TableField("`describe`")
    private String describe;

    @ApiModelProperty("总数量")
    @TableField("total")
    private Integer total;

    @ApiModelProperty("已领取的数量")
    @TableField("issued_count")
    private Integer issuedCount;

    @ApiModelProperty("已被使用的数量")
    @TableField("used_count")
    private Integer usedCount;

    @ApiModelProperty("优惠卷类型(0满减/无门槛卷/代金券，1折扣卷)")
    @TableField("type")
    private Byte type;

    @ApiModelProperty("发行的类型（0平台卷、1店铺卷）")
    @TableField("issue_type")
    private Byte issueType;

    @ApiModelProperty("膨胀规则-为空不可膨胀{\"膨胀后金额\":概率值,...}（issue_type=0）")
    @TableField(value = "expansion_rule")
    private String expansionRule;

    @ApiModelProperty("店铺参与的标志/店铺参与的平台活动标志（使用平台膨胀卷时需要看店铺是否有这个授权标志）")
    @TableField("shop_participation_flag")
    private String shopParticipationFlag;

    @ApiModelProperty("最低消费多少金额可用优惠券（门槛）")
    @TableField("threshold_amount")
    private BigDecimal thresholdAmount;

    /**
     * 如果是折扣率，那其实是0.01~9.99 如果要拿来算需要除于10
     */
    @ApiModelProperty("优惠卷值(根据type：抵扣金额 | 折扣率)")
    @TableField("par_value")
    private BigDecimal parValue;

    @ApiModelProperty("获取方式（  -1:兑换码 , 1:店铺公开展示-手动领取. 2:非公开群内手动领取  ）")
    @TableField("obtain_way")
    private Byte obtainWay;

    @ApiModelProperty("每人限领张数")
    @TableField("per_person_limit")
    private Integer perPersonLimit;

    @ApiModelProperty("使用有效期类型（1:固定日期 2:相对有效期）")
    @TableField("usage_validity_period_type")
    private Byte usageValidityPeriodType;

    @ApiModelProperty("自领取之日起有效天数（单位：天）")
    @TableField(value = "usage_period_days", updateStrategy = FieldStrategy.IGNORED)
    private Integer usagePeriodDays;

    @ApiModelProperty("开始使用时间")
    @TableField(value = "usage_start_time", updateStrategy = FieldStrategy.IGNORED)
    private LocalDateTime usageStartTime;

    @ApiModelProperty("结束使用时间")
    @TableField(value = "usage_end_time", updateStrategy = FieldStrategy.IGNORED)
    private LocalDateTime usageEndTime;

    @ApiModelProperty("适用类型（0:无适用限制 1:适用类别 2:适用商品）")
    @TableField("applicable_type")
    private Byte applicableType;

    @ApiModelProperty("适用逻辑（1:适用选择 2:不适用选择的）")
    @TableField("applicable_logic")
    private Byte applicableLogic;

    @ApiModelProperty("适用值（选择的类目ids或商品ids）")
    @TableField("applicable_values")
    private String applicableValues;

    @ApiModelProperty("是否可以叠加使用(0 不可叠加 1可叠加)")
    @TableField("is_stackable")
    private Boolean isStackable;

    @ApiModelProperty("更新时间")
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    @ApiModelProperty("创建时间")
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    /**
     * 逻辑删除
     */
    @ApiModelProperty(value = "逻辑删除")
    @TableField(value = "deleted", fill = FieldFill.INSERT)
    @TableLogic
    private Integer deleted;

    /**
     * 如果是折扣券，则返回真实的折扣率
     *
     * @return
     */
    public BigDecimal ifDiscountCouponParValue() {
        return Objects.equals(this.type, CouponType.DISCOUNT.getCode()) ? this.getParValue().divide(BigDecimal.valueOf(10)) : this.getParValue();
    }
}
